using UnityEngine;
using System.Collections;

public class SoundWaveEffect : MonoBehaviour {

    public LineRenderer m_InnerRing;
    public LineRenderer m_OuterRing;
    public int m_VertexAmount;

    float m_InnerRadius = 0;
    float m_Radius;
    Vector3 m_Center;
    bool m_Started = false;
    float m_GrowingSpeed;
	void Awake () 
    {
        //m_OuterRing.SetColors(Color.yellow, Color.yellow);
        m_InnerRing.SetVertexCount(m_VertexAmount+1);
        m_OuterRing.SetVertexCount(m_VertexAmount +1);
	}

    void Start()
    {
        //StartEffect(this.transform.position, 10, 15);
    }
	void Update () 
    {
        if (m_Started == false) return;
        if (VisionManager.GetInstance().IsSoundVisualActive() == false)
        {
            EndEffect();
        }
        if (m_InnerRadius < m_Radius)
        {
            m_InnerRadius += (Time.deltaTime * m_GrowingSpeed);
            SetCirclePoints(m_InnerRing, m_InnerRadius, m_Center);
        }
        else
        {
            EndEffect();
        }
	}
    void SetCirclePoints(LineRenderer lineRenderer, float radius, Vector3 center)
    {

        float anglePerVertex = (Mathf.PI * 2 /(m_VertexAmount));
        for (int i = 0; i < m_VertexAmount + 1; i++)
        {
            Vector3 pos;
            pos.x = Mathf.Cos(anglePerVertex * i) * radius;
            pos.y = 0;
            pos.z = Mathf.Sin(anglePerVertex * i) * radius;
            lineRenderer.SetPosition(i, center + pos);
        }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="center">Centro</param>
    /// <param name="radius">Radio del efecto</param>
    /// <param name="growSpeed">Velocidad a la que se agranda el circulo del medio.</param>
    public void StartEffect(Vector3 center, float radius, float growSpeed)
    {
        m_Center = center;
        m_Radius = radius;
        m_InnerRadius = 0;
        m_GrowingSpeed = growSpeed;
        m_Started = true;
        SetCirclePoints(m_OuterRing, radius, center);
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="duration">Duracion del efecto</param>
    /// <param name="radius">Radio de los circulos</param>
    /// <param name="center">Centro</param>
    public void StartEffect(float duration, float radius, Vector3 center)
    {
        m_Center = center;
        m_Radius = radius;
        m_InnerRadius = 0;
        m_GrowingSpeed = radius / duration;
        m_Started = true;
        SetCirclePoints(m_OuterRing, radius, center);
    }
    void EndEffect()
    {
        m_Started = false;
        SetCirclePoints(m_InnerRing, 0, m_Center);
        PrefabManager.GetInstance().RecycleToPool("soundwave", this.gameObject);
    }
}
